import axios from "axios"

class Http {
    constructor() {
        this.instance = axios.create({
            baseURL: import.meta.env.VITE_BASE_URL,
            timeout: 6000
        });
    }

    post(path, data) {
        return new Promise(async (resolve, reject) => {
            try {
                let result = await this.instance.post(path, data)
                resolve(result.data)
            } catch (err) {
                try {
                    let detail = err.response.data.detail;
                    reject(detail)
                } catch {
                    reject('服务器错误！')
                }
            }
        })
    }

    get(path, params) {
        return new Promise(async (resolve, reject) => {
            try {
                let result = await this.instance.get(path, {params})
                resolve(result.data)
            } catch (err) {
                let detail = err.response.data.detail;
                reject(detail)
            }
        })
    }

    put(path, data) {
        return new Promise(async (resolve, reject) => {
            try {
                let result = await this.instance.put(path, data)
                resolve(result.data);
            } catch (err) {
                let detail = err.response.data.detail;
                reject(detail)
            }
        })
    }

    delete(path) {
        return new Promise(async (resolve, reject) => {
            try {
                let result = await this.instance.delete(path)
                resolve(result.data);
            } catch (err) {
                let detail = err.response.data.detail;
                reject(detail)
            }
        })
    }


}


export default new Http()